package cn.easybuy.filter;


import java.io.IOException;

import javax.servlet.Filter;
import javax.servlet.FilterChain;
import javax.servlet.FilterConfig;
import javax.servlet.ServletException;
import javax.servlet.ServletRequest;
import javax.servlet.ServletResponse;

public class CharacterEncodingFilter implements Filter{
	private String charset=null;


	public void destroy() {
		System.out.println("执行字符过滤器的destroy()方法");
		
	}

	public void doFilter(ServletRequest request, ServletResponse response,
			FilterChain chain) throws IOException, ServletException {
		// TODO Auto-generated method stub
		System.out.println("执行字符过滤器的doFilter()方法");
		if(charset!=null){
			request.setCharacterEncoding(charset);
			response.setContentType("text/html;charset="+charset);
			
		}
		chain.doFilter(request,response);
		
	}

	public void init(FilterConfig filterConfig) throws ServletException {
		// TODO Auto-generated method stub
		System.out.println("执行字符过滤器的init()方法");
		String initparam=filterConfig.getInitParameter("charset");
		if(initparam!=null&&(initparam=initparam.trim()).length()!=0){
			charset=initparam;
			
		}
		
	}
}
